﻿#region
/*******************************************************************************
** File Name:		    NewsCatalogService.cs
** Creator:		        chao.jiang 
** EMail:               chao.jiang 
** Create date:	        
** Latest Modifier:
** Latest Modify date:
** Description:		    
** Modify History:
**
** Code Review:
**
** Version number:       1.0.0
*********************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using Phoenix.Service.Contracts;
using Phoenix.Service.Model;
using Phoenix.IBatisRepository;
using Phoenix.Respository;
using System.Data;
using Phoenix.Entity;
using System.Collections;

namespace Phoenix.Service
{
    public class NewsCatalogService : INewsCatalogService
    {
		 INewsCatalog newsCatalog = new NewsCatalogRepository();
		 
		 #region  Auto Generation
		 
        public System.Int32 AddNewsCatalog(NewsCatalogModel model)
        {
			ENewsCatalog entity = ConvertModelToEntity(model);
			
            return newsCatalog.AddNewsCatalog(entity);
        }

		public void UpdateByCatalogID (NewsCatalogModel model)
		{
			ENewsCatalog entity = ConvertModelToEntity(model);

            newsCatalog.UpdateByCatalogID(entity);

		}

		public void DeleteByCatalogID (System.Int32 catalogID)
		{
		
            newsCatalog.DeleteByCatalogID(catalogID);

		}
		 
        public NewsCatalogModel GetByCatalogID (System.Int32 catalogID)
        {
        
			ENewsCatalog entity = newsCatalog.GetByCatalogID(catalogID);
			
            NewsCatalogModel model = ConvertEntityToModel(entity);


            return model;
        }
        
		public NewsCatalogModel ConvertEntityToModel(ENewsCatalog entity)
		{
			if (entity == null)
            {
                return null;
            }
			NewsCatalogModel model = new NewsCatalogModel();
                        model.CatalogID = entity.CatalogID;
                        model.CatalogName = entity.CatalogName;
                        model.ParentID = entity.ParentID;
                        model.IDPath = entity.IDPath;
                        model.NamePath = entity.NamePath;
                        model.Memo = entity.Memo;
                        model.SortOrder = entity.SortOrder;
                        model.NewsCount = entity.NewsCount;
                        model.DisplayFlag = entity.DisplayFlag;
                        model.IsLeaf = entity.IsLeaf;
                        model.CreateTime = entity.CreateTime;
                        model.CreateUser = entity.CreateUser;
                        model.UpdateTime = entity.UpdateTime;
                        model.UpdateUser = entity.UpdateUser;
                        return model;
		}
		
		public ENewsCatalog ConvertModelToEntity(NewsCatalogModel model)
		{
			if (model == null)
            {
                return null;
            }
			ENewsCatalog entity = new ENewsCatalog();
                        entity.CatalogID = model.CatalogID;
                        entity.CatalogName = model.CatalogName;
                        entity.ParentID = model.ParentID;
                        entity.IDPath = model.IDPath;
                        entity.NamePath = model.NamePath;
                        entity.Memo = model.Memo;
                        entity.SortOrder = model.SortOrder;
                        entity.NewsCount = model.NewsCount;
                        entity.DisplayFlag = model.DisplayFlag;
                        entity.IsLeaf = model.IsLeaf;
                        entity.CreateTime = model.CreateTime;
                        entity.CreateUser = model.CreateUser;
                        entity.UpdateTime = model.UpdateTime;
                        entity.UpdateUser = model.UpdateUser;
                        return entity;
		}
		
		#endregion

        public IList<NewsCatalogModel> GetByParentID(System.Int32 parentId)
        {
            IList<ENewsCatalog> entityList = newsCatalog.GetByParentID(parentId);
            if(entityList==null)
                return null;

            IList<NewsCatalogModel> modelList = new List<NewsCatalogModel>();
            foreach (var item in entityList)
            {
                modelList.Add(ConvertEntityToModel(item));
            }

            return modelList;
        }

        public Hashtable GetIdNameByParentID(System.Int32 parentId)
        {
            IList<ENewsCatalog> entityList = newsCatalog.GetByParentID(parentId);
            if (entityList == null)
                return null;
            Hashtable ht = new Hashtable();
            IList<NewsCatalogModel> modelList = new List<NewsCatalogModel>();
            foreach (var item in entityList)
            {
                ht.Add(item.CatalogID, item.CatalogName);
            }
            return ht;
        }

    }
}